package com.seckill.act.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.heima.minio.MinIOTemplate;
import com.seckill.act.service.SkuPageService;
import com.seckill.goods.pojo.Sku;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Administrator
 * @Date 2023/9/25
 **/
@Service
@Slf4j
public class SkuPageServiceImpl implements SkuPageService {

    @Autowired
    private Configuration configuration;
    @Autowired
    private MinIOTemplate minIOTemplate;

    @Override
//    public List<String> skuPage(List<Sku> skus) throws IOException {
//        log.info("生成静态详情页面线程:{}",Thread.currentThread().getName());
//        //1. 加载商品静态页面模板
//        Template template = configuration.getTemplate("item.ftl");
//        List<String> urlList = new ArrayList<>(skus.size());
//        //2. 遍历获取每一个商品, 为每一个商品生成静态页面
//        skus.stream().forEach(sku -> {
//            //封装数据模型
//            Map<String, Object> dataModel = new HashMap<>(1);
//            dataModel.put("sku", sku);
//            //生成静态页面
//            try {
//                //输出静态文件到本地
//                template.process(dataModel,new FileWriter("D:\\code\\project\\seckill-web\\nginx-seckill\\seckill-app\\pages\\items"+sku.getId()+".html"));
//                urlList.add("http://app-seckill-java.itheima.net/items/" + sku.getId() + ".html");
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }
//        });
//
//        return urlList;
//    }

    public List<Sku> skuPage(List<Sku> skus) throws IOException {
        if (CollUtil.isEmpty(skus)) {
            return null;
        }
        String name = Thread.currentThread().getName();

        //获取模板
        Template template = configuration.getTemplate("item.ftl");
        skus.stream().forEach(sku -> {
            log.info("由现成" + name + "执行" + sku.getId() + "的静态化操作!");
            Map<String, Object> dataModel = new HashMap<>(1);
            dataModel.put("sku", sku);
            //将数据填充到模板中
            try {
                //创建输出流
                //将模板中的数据导入到这个输出流中
                StringWriter stringWriter = new StringWriter();
                template.process(dataModel, stringWriter);
                String html = stringWriter.getBuffer().toString();

                //再将数据输出到MinIO中即可
                String minIOUrl = minIOTemplate.uploadHtmlFile("html", sku.getId() + ".html", new ByteArrayInputStream(html.getBytes(StandardCharsets.UTF_8)));
                log.info(minIOUrl);
                sku.setHtmlUrl(minIOUrl);
            } catch (Exception e) {
                throw new RuntimeException("商品" + sku.getId() + "静态化数据失败, 原因为:" + e.getMessage());
            }
        });
        return skus;
    }
}
